package com.yufuchang;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Created by yufuchang on 2020/8/29.
 */
@RestController
public class PostController {

    @PostMapping("/hello_post")
    public String helloPost(@RequestParam("name") String name,@RequestParam("") Integer age){
        return "我的名字是："+name+"我的年龄是："+age;
    }

    @PostMapping("/hello_post_default")
    public String helloPostWithDefaultValue(@RequestParam(value = "name",defaultValue = "于富昌") String name,@RequestParam(value = "age",required = false) Integer age){
        return "默认值我的名字是："+name+"我的年龄是："+age;
    }

    @PostMapping("/hello_post_map")
    public String postWithMap(@RequestParam Map<String,String> map){
        return "name：" + map.get("name") + "\nage：" + map.get("age");
    }

    @PostMapping("/post_array")
    public String postWithArray(@RequestParam("name") String[] names){
        String result = "";
        for(String name:names){
            result += name + "\n";
        }
        return result;
    }

    /**
     * @param user
     * @return
     */
    @PostMapping("post_object")
    public String postObject(User user){
        return "name：" + user.getName() + "\nage：" + user.getAge();
    }
}
